/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.imp.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.apache.log4j.Logger;

import who.tmap.bo.AttrDef;
import who.tmap.bo.impl.BizObjectFactory;
import who.tmap.common.TMapException;
import who.tmap.imp.ImportAdapterException;
import who.tmap.toolkit.impl.AttrHelper;

public abstract class BaseJDBCAdapterImpl extends BaseImportAdapterImpl {
	
    private static Logger logger = Logger.getLogger(BaseJDBCAdapterImpl.class);    	

    protected static final String StringType =String.class.getName();		
	
    protected Connection conn =null;
    protected String tableName;    
    protected String url;
    protected boolean useBrackets;      
	
	/**
 	 * Retrieve the list of tables from the data source schema.
 	 * A sheet stands for a table in Excel case
	 * @param schema
	 * @return
	 * @throws TMapException
	 */
    public Set<String> getTables() throws TMapException {
        Set<String> tables =new TreeSet<String>();
        ResultSet rs =null;
        
        try { 
            rs =conn.getMetaData().getTables( null, null, "%"
            								, new String[] { "TABLE", "VIEW" } );
            while( rs.next() ) {
                tables.add( rs.getString("TABLE_NAME") );
            }
            
        } catch( Exception e ) {
        	try { 
        		rs.close();
        	} catch( Exception e2 ) {
        	}
            throw new ImportAdapterException( e );
        }
        
        return tables;
    }

    /**
     * 
     */
    public List<AttrDef> readHeader() throws ImportAdapterException {
        List<AttrDef> fields =new ArrayList<AttrDef>();
        
        AttrHelper attrHelper =new AttrHelper();
        ResultSet rs =null;
        
        try {
            rs =conn.getMetaData().getColumns( null, null, tableName, "%" );
            while( rs.next() ) {
                AttrDef attrDef =(AttrDef)boFactory.get( BizObjectFactory.ATTR_DEF );
                
                attrDef.setName( rs.getString( "COLUMN_NAME" ) );
                attrDef.setType( attrHelper.getType( rs.getShort( "DATA_TYPE" ) ) );
                // Size is useful for casting string for SQL Server
                attrDef.setSize( rs.getInt( "COLUMN_SIZE") );
                
                fields.add( attrDef );
            }
        } catch( Exception e ) {
        	try { 
        		rs.close();
        	} catch( Exception e2 ) {
        	}        	
            throw new ImportAdapterException( e );
        };
        
        return fields;
    }
    
    protected String getBracketField( String fieldName ) {
    	String bField;
    	if ( useBrackets ) {
    		bField ="[" + fieldName + "]";
    	} else {
    		bField =fieldName;
    	}
    	
    	return bField;
    }    
	
}
